import { Notify, Toast } from 'vant';

const requestHttp = (method = 'GET', api: string, data: any = null, headers= {"Content-Type": "application/json"} ) => {
    console.log(api)
    return new Promise((resolve,reject) => {
        fetch(api, {...{
            method: method.toUpperCase(),
            headers: {
                authorization: `bearer ${localStorage.getItem('token')}`,
                ...headers
            },
            mode: 'cors'
        },...(data === null ? {} : {body: JSON.stringify(data)})}).then(rs=>{
            return rs.json() 
        }).then(res=>{
            if (res.code == 0) {
                resolve(res)
            }else{
                Notify({ type: 'warning', message: res.msg});
                reject()
            }
        }).catch((res)=>{
            console.log(res)
            Notify({ type: 'warning', message: '接口请求失败'});
            reject()
        })
    }) 
}

export {requestHttp};
